package com.cn.clp.weChat;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class CustomMenu {
    public static void createCustomMenu() throws Exception{
        //添加菜单
        String custmMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=";
        //删除菜单
      //  String custmMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";

        //获取access_token
        String accessToken = new AccessToken().getAccessToken();
        custmMenuUrl = custmMenuUrl + accessToken;

        URL url = new URL(custmMenuUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.connect();

        OutputStream outputStream = connection.getOutputStream();
        outputStream.write(getMenuStr().getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();

        InputStream inputStream = connection.getInputStream();
        int size =inputStream.available();
        byte[] bs =new byte[size];
        inputStream.read(bs);
        String message=new String(bs,"UTF-8");

        System.out.println(message);
    }

    private static String getMenuStr() {
        JSONObject firstLevelMenu = new JSONObject();//一级菜单
        JSONArray firstLevelMenuArray = new JSONArray();//一级菜单列表


        //一级菜单内容
        JSONObject firstLevelMenuContext1 = new JSONObject();
        firstLevelMenuContext1.put("type", "click");
        firstLevelMenuContext1.put("name", "菜单一");
        firstLevelMenuContext1.put("key", "V1001_TODAY_MUSIC");

        //一级菜单内容2
        JSONObject firstLevelMenuContext2 = new JSONObject();
        //一级菜单内容2的二级菜单列表
        JSONArray firstLevelMenuContext2Array = new JSONArray();
        //一级菜单内容2的二级菜单内容1
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("type", "view");
        jsonObject1.put("name", "同行大厅");
        jsonObject1.put("url", "http://exam.wxcrm.anshipai.com?name=peer");
        //一级菜单内容2的二级菜单内容2
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("type", "view");
        jsonObject2.put("name", "人脉圈");
        jsonObject2.put("url", "http://exam.wxcrm.anshipai.com?name=contacts");
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("type", "view");
        jsonObject3.put("name", "个人中心");
        jsonObject3.put("url", "http://exam.wxcrm.anshipai.com?name=personal");
        firstLevelMenuContext2Array.add(jsonObject1);
        firstLevelMenuContext2Array.add(jsonObject2);
        firstLevelMenuContext2Array.add(jsonObject3);
        firstLevelMenuContext2.put("name", "安石派");
        firstLevelMenuContext2.put("sub_button", firstLevelMenuContext2Array);

        //一级菜单内容3
        JSONObject firstLevelMenuContext3 = new JSONObject();
        firstLevelMenuContext3.put("type", "view");
        firstLevelMenuContext3.put("name", "菜单三");
        firstLevelMenuContext3.put("url", "https://www.baidu.com");


        firstLevelMenuArray.add(firstLevelMenuContext1);
        firstLevelMenuArray.add(firstLevelMenuContext2);
        firstLevelMenuArray.add(firstLevelMenuContext3);


        firstLevelMenu.put("button", firstLevelMenuArray);

        return firstLevelMenu.toString();
    }
    public static void deleteMenu() throws Exception {
        String custmMenuUrl = "https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=";
        String accessToken = new AccessToken().getAccessToken();
        custmMenuUrl = custmMenuUrl + accessToken;

        URL url = new URL(custmMenuUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.connect();

        InputStream inputStream = connection.getInputStream();
        int size =inputStream.available();
        byte[] bs =new byte[size];
        inputStream.read(bs);
        String message=new String(bs,"UTF-8");

        System.out.println(message);
    }

    public static void main(String[] args) throws Exception {
       createCustomMenu();
       //deleteMenu();
    }
}
